namespace SceneManagement {
    let _layerStack: Array<any> = [];

    let _currentLayer: any = null;

    let _routes: Array<{ path: string, layer: any }>;

    let _stage: any = null;
    /**
     *
     *
     * @export
     * @param {string} path
     */
    export function go(path: string,params:any) {
        let route = _routes.filter(value => {
            return value.path == path;
        })[0];
        _layerStack.push(route);
        if (_currentLayer != null) {
            removeLayer(_stage, _currentLayer)
            _currentLayer.routerLeave();
        }

        let _to = new route.layer();
        _to.routerIn(_currentLayer,_to,params)
        addLayer(_stage, _to);
        _currentLayer=_to;
        _to=null;
    }



    /**
     *
     *
     * @export
     * @param {number} index
     */
    export function forward(index: number) {

    }



    /**
     *
     *
     * @export
     * @param {number} index
     */
    export function back(index: number) {

    }

    export function initRoutes(routes: Array<{ path: string, layer:any}>, stage: any) {
        _stage = stage;
        _routes = routes;
    }
}


function addLayer(stage: any, layer: any) {
    stage.addChild(layer);
    layer.routerAfterIn();
}

function removeLayer(stage: any, layer: any) {
    stage.removeChild(layer);
}